//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.mapper;

import com.example.entity.Club;
import com.example.entity.Clubmember;
import com.example.entity.ClubApplication;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ClubMapper {
    List<Club> selectAll(Club club);

    @Select("SELECT c.*, s.name as imageUploaderName FROM club c " +
            "LEFT JOIN student s ON c.image_uploader_id = s.id " +
            "WHERE c.id = #{id}")
    Club selectById(Integer id);

    @Insert({"insert into club (name,descr,president_id) values (#{name},#{descr},#{presidentId})"})
    int insert(Club club);//后端_，前端驼峰


    int updateById(Club club);

    @Update("update club set image_url = #{imageUrl}, image_upload_time = #{imageUploadTime}, image_uploader_id = #{imageUploaderId} where id = #{id}")
    int updateImageUrl(Club club);

    @Delete({"delete from club where id = #{id}"})
    int deleteById(Integer id);

    @Select("select * from club where president_id = #{presidentId}")
    List<Club> selectByPresidentId(Integer presidentId);

    @Select("select count(*) from club_member where club_id = #{clubId} and student_id = #{studentId}")
    int checkMemberExists(Integer clubId, Integer studentId);

    @Select("select count(*) from club_application where club_id = #{clubId} and student_id = #{studentId} and status = '待审核'")
    int checkApplicationExists(Integer clubId, Integer studentId);

    @Insert("INSERT INTO club_application (club_id, student_id, apply_time, status) " +
            "VALUES (#{clubId}, #{studentId}, #{applyTime}, #{status})")
    int createApplication(ClubApplication application);

    @Insert("insert into club_member (club_id, student_id, president_id, join_time) values (#{clubId}, #{studentId}, (select president_id from club where id = #{clubId}), now())")
    int addMember(Integer clubId, Integer studentId);

    @Delete("DELETE FROM club_member WHERE club_id = #{clubId} AND student_id = #{studentId}")
    int deleteMember(@Param("clubId") Integer clubId, @Param("studentId") Integer studentId);

    @Select("SELECT c.* FROM club c " +
            "INNER JOIN club_member cm ON c.id = cm.club_id " +
            "WHERE cm.student_id = #{studentId}")
    List<Club> selectByStudentId(Integer studentId);

    @Select("SELECT cm.id, cm.club_id as clubId, cm.student_id as studentId, " +
            "cm.join_time as joinTime, s.name as studentName " +
            "FROM club_member cm " +
            "LEFT JOIN student s ON cm.student_id = s.id " +
            "WHERE cm.club_id = #{clubId}")
    List<Clubmember> getMembersByClubId(Integer clubId);
}
